package com.xunkids.bos.migration.business.controller.system;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xunkids.bos.migration.common.constant.BusinessConstant;
import com.xunkids.bos.migration.component.transmission.SystemExecutorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @program: bos-migration
 * @author: byl
 * @create: 2024-04-03 13:47
 * @Description
 */

@Api(tags = "系统参数调整")
@RestController
@RequestMapping("/system")
public class SystemController {

    @ApiOperation("获取百度BOS信息")
    @PostMapping("/getBaiDuBOSInfo")
    public Map<String,String> getBaiDuBOSInfo() {
        Map<String,String> result = MapUtil.newHashMap();
        result.put("access_key",BusinessConstant.BAIDU_ACCESS_KEY_ID);
        result.put("access_key_secret",BusinessConstant.BAIDU_ACCESS_KEY_SECRET);
        result.put("end_point",BusinessConstant.BAIDU_END_POINT);
        return result;
    }


    @ApiOperation("配置百度BOS信息")
    @PostMapping("/putBaiDuBOSInfo")
    public String putBaiDuBOSInfo(@RequestBody Map<String,String> bosInfo) {
        BusinessConstant.BAIDU_ACCESS_KEY_ID = bosInfo.get("access_key");
        BusinessConstant.BAIDU_ACCESS_KEY_SECRET = bosInfo.get("access_key_secret");
        BusinessConstant.BAIDU_END_POINT = bosInfo.get("end_point");
        return "success";
    }


    @ApiOperation("获取小米ali-oss信息")
    @PostMapping("/getXIAOMIOSSInfo")
    public Map<String,String> getXIAOMIOSSInfo() {
        Map<String,String> result = MapUtil.newHashMap();
        result.put("access_key",BusinessConstant.XIAOMI_ACCESS_KEY_ID);
        result.put("access_key_secret",BusinessConstant.XIAOMI_ACCESS_KEY_SECRET);
        result.put("end_point",BusinessConstant.XIAOMI_END_POINT);
        result.put("regoin_id",BusinessConstant.XIAOMI_REGOIN_ID);
        result.put("prefix",BusinessConstant.XIAOMI_BUCKET_PREFIX);
        result.put("suffix",BusinessConstant.XIAOMI_BUCKET_SUFFIX);
        return result;
    }


    @ApiOperation("配置小米ali-oss信息")
    @PostMapping("/putXIAOMIOSSInfo")
    public String putXIAOMIOSSInfo(@RequestBody Map<String,String> bosInfo) {
        BusinessConstant.XIAOMI_ACCESS_KEY_ID = bosInfo.get("access_key");
        BusinessConstant.XIAOMI_ACCESS_KEY_SECRET = bosInfo.get("access_key_secret");
        BusinessConstant.XIAOMI_END_POINT = bosInfo.get("end_point");
        BusinessConstant.XIAOMI_REGOIN_ID = bosInfo.get("regoin_id");
        BusinessConstant.XIAOMI_BUCKET_PREFIX = bosInfo.get("prefix");
        BusinessConstant.XIAOMI_BUCKET_SUFFIX = bosInfo.get("suffix");
        return "success";
    }

    @ApiOperation("查询米兔得设备类型")
    @PostMapping("/queryMiTuDeviceType")
    public Object queryMiTuDeviceType(@RequestParam(value = "deviceType",required = false) String deviceType) {
        if(StrUtil.isNotEmpty(deviceType)){
            return  BusinessConstant.MI_TU_DEVICE_TYPE.contains(deviceType);
        }
        return  BusinessConstant.MI_TU_DEVICE_TYPE ;
    }

    @ApiOperation("配置米兔得设备类型(type == 0 添加 ，其余删除)")
    @PostMapping("/addOrDelMiTuDeviceType")
    public Object addOrDelMiTuDeviceType(@RequestParam(value = "deviceType",required = false) String deviceType, @RequestParam("type") Integer type) {
        if(type == 1){ //添加
            return  BusinessConstant.MI_TU_DEVICE_TYPE.add(deviceType);
        }
        return  BusinessConstant.MI_TU_DEVICE_TYPE.remove(deviceType) ;
    }

    @ApiOperation("查询米兔优先迁移的设备类型")
    @PostMapping("/queryMiTuFirstDeviceType")
    public Object queryMiTuFirstDeviceType() {
        return  BusinessConstant.FIRST_DEVICE_TYPE ;
    }


    @ApiOperation("配置米兔优先迁移的设备类型(type == 0 添加 ，其余删除)")
    @PostMapping("/addOrDelMiTuFirstDeviceType")
    public Object addOrDelMiTuFirstDeviceType(@RequestParam(value = "deviceType",required = false) String deviceType, @RequestParam("type") Integer type) {
        if(type == 1){ //添加
            BusinessConstant.FIRST_DEVICE_TYPE= deviceType;
            return "success";
        }
        return  BusinessConstant.FIRST_DEVICE_TYPE = null ;
    }



    @ApiOperation("查看或修改小米消费者URL")
    @PostMapping("/consumerUrl")
    public String consumerUrl(@RequestParam(value = "consumerUrl",required = false) String consumerUrl) {
        if(ObjectUtil.isNotEmpty(consumerUrl)){
            BusinessConstant.XIAOMI_CONSUMER_URL = consumerUrl;
        }
        return BusinessConstant.XIAOMI_CONSUMER_URL;
    }

    @ApiOperation("打印内存队列信息")
    @PostMapping("/printQueueInfo")
    public String printQueueInfo() {
        return SystemExecutorService.printQueueInfo();
    }

    @ApiOperation("是否打开存量同步")
    @PostMapping("/stockSwitch")
    public Boolean stockSwitch(@RequestParam(value = "stockSwitch",required = false) Boolean stockSwitch) {
        if(ObjectUtil.isNotEmpty(stockSwitch)){
            BusinessConstant.STOCK_SYNC_SWITCH = stockSwitch;
        }
         return BusinessConstant.STOCK_SYNC_SWITCH;
    }


    @ApiOperation("设置和查看存量同步得截至时间")
    @PostMapping("/stockLocalDateTime")
    public LocalDate stockLocalDateTime(@RequestParam(value = "stockLocalDateTime",required = false) String stockLocalDateTime) {

        if(ObjectUtil.isNotEmpty(stockLocalDateTime)){
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            BusinessConstant.INVENTORY_FILE_STORAGE_END_TIME = LocalDate.parse(stockLocalDateTime,df);
        }
        return BusinessConstant.INVENTORY_FILE_STORAGE_END_TIME;
    }



}
